package com.ctg.payroll2.action.cusadmin;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.ctg.payroll2.bean.po.SysUser;
import com.ctg.payroll2.service.cusadmin.PbCompanyService;
import com.ctg.payroll2.util.Constants;
import com.ctg.platform.util.Page;
import com.ctg.platform.util.Parse;
import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class PbCompanyAction extends ActionSupport {

	private PbCompanyService companyService;
	private Page products;
	private String stopReason;

	public String getStopReason() {
		return stopReason;
	}

	public void setStopReason(String stopReason) {
		this.stopReason = stopReason;
	}

	public Page getProducts() {
		return products;
	}

	public void setProducts(Page products) {
		this.products = products;
	}

	@JSON(serialize = false)
	public PbCompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(PbCompanyService companyService) {
		this.companyService = companyService;
	}

	private Page result;

	public Page getResult() {
		return result;
	}

	public void setResult(Page result) {
		this.result = result;
	}

	private String checkUserCode;

	public String getCheckUserCode() {
		return checkUserCode;
	}

	public void setCheckUserCode(String checkUserCode) {
		this.checkUserCode = checkUserCode;
	}

	/**
	 * 客户注册登录页面的数据显示，默认从数据库查出所有信息并且分页显示
	 * 
	 * @return
	 */
	public String getAllPbCompany() {
		try {
			String company_code = null;
			String company_chinese_code = null;
			Integer status = null;
			String start_date = null;
			String end_date = null;
			Integer start = null;
			Integer limit = null;
			HttpServletRequest request = ServletActionContext.getRequest();
			company_code = Parse.toString(request.getParameter("company_code"),
					false);
			company_chinese_code = Parse.toString(request
					.getParameter("company_chinese_code"), false);
			status = Parse.toInteger(request.getParameter("state"), 1);
			if (request.getParameter("startdt") != null) {
				start_date = request.getParameter("startdt");
			}
			if (request.getParameter("enddt") != null) {
				end_date = request.getParameter("enddt");
			}
			start = Parse.toInteger(request.getParameter("start"), 0);
			limit = Parse.toInteger(request.getParameter("limit"), 10);

			result = companyService.getAllPbCompanyByPaging(company_code,
					company_chinese_code, status, start_date, end_date, start,
					limit);
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("company_code", company_code);
			List<HashMap<String, Object>> sropList = companyService
					.select_hisstop(map);
			String stopStr = "";
			for (HashMap<String, Object> hashMap : sropList) {
				stopStr += "停用原因 : " + hashMap.get("END_REASON") + "\n停用时间 : "
						+ hashMap.get("END_TIME") + "\n停用人 : "
						+ hashMap.get("END_USER") + "\n\n";
			}
			stopReason = stopStr;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 客户从停用到启用的状态更新
	 * 
	 * @return
	 */
	public String updateStatusByCompanyCode() {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			String company_code = Parse.toString(request
					.getParameter("companyCode"), false);
			companyService.updateStatus(company_code);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * @return 保存公司信息
	 */
	@SuppressWarnings("unchecked")
	/**
	 * isSig company_code是否已存在
	 */
	String isSig;

	public String saveCompany() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String str = request.getParameter("companystr");
		try {
			if (str != null) {
				if (!companyService.insert(str)) {
					isSig = "该客户编号已存在!";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String TestCompanyCode() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String companyCode = request.getParameter("companyCode");
		if (companyService.isSig(companyCode)) {
			isSig = "该客户编号已存在!";
		}
		return SUCCESS;
	}

	/**
	 * 修改公司信息
	 * 
	 * @return
	 */
	public String goUpdate() {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			String str = request.getParameter("companystr");
			String company_code = request.getParameter("company_code");
			String stop_reason = request.getParameter("stop_reason");
			if (str != null) {
				companyService.update(str);
			}
			if ("" != stop_reason && null != stop_reason) {
				HashMap<String, Object> paraMap = new HashMap<String, Object>();
				SysUser sysuer = (SysUser) request.getSession().getAttribute(
						Constants.CURRENT_USER);
				if (null != sysuer) {
					if (null != company_code) {
						paraMap.put("company_code", company_code);
						paraMap.put("end_reason", stop_reason);
						paraMap.put("end_user", sysuer.getLoginId());
						companyService.insertstop_reason(paraMap);
					} else {
						throw new RuntimeException("客户无效");
					}
				} else {
					throw new RuntimeException("session 过期");
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;

	}

	public String deleteLinkMan() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String deletelinkmans = (String) request
				.getParameter("deletelinkmanpar");
		if (deletelinkmans != null) {
			companyService.deleteLinkMan(deletelinkmans);
		}
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String queryProduct() {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			String company_code = Parse.toString(request
					.getParameter("company_code"), false);
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("company_code", company_code);
			products = companyService.getProduct(map, 0, 100);
//			System.out.println(1222);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String getIsSig() {
		return isSig;
	}

	public void setIsSig(String isSig) {
		this.isSig = isSig;
	}

}
